/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestLAF.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.awt;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * Look and Feel
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestLAF {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				LookAndFeelFrame frame = new LookAndFeelFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class LookAndFeelFrame extends JFrame {

	private static final long serialVersionUID = 8565440397398534220L;

	private JPanel feelPanel = null;

	public LookAndFeelFrame() {
		// 设置Frame的基本信息
		this.setSize(600, 100);
		this.setTitle("Look And Feel");
		this.setLocationByPlatform(true);
		this.setResizable(false);

		feelPanel = new JPanel();

		// 获得所有可用的感官类
		UIManager.LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels();
		for (UIManager.LookAndFeelInfo lnf : lnfs) {
			this.generateButton(lnf.getName(), lnf.getClassName());
		}

		this.getContentPane().add(feelPanel);
	}

	/**
	 * 生成感官按钮
	 * 
	 * @param buttonName
	 *            按钮名称
	 * @param lnfClassName
	 *            感官类名称
	 */
	public void generateButton(String buttonName, final String lnfClassName) {
		JButton button = new JButton(buttonName);
		this.feelPanel.add(button);

		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					UIManager.setLookAndFeel(lnfClassName);// 设置感官类
					SwingUtilities.updateComponentTreeUI(LookAndFeelFrame.this);// 刷新所有组件
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
	}
}